﻿using DataBaseConnect.IRepositories;
using Entity;
using System;
using Utils;

namespace DataBaseConnect.Repositories
{
    public class MessageRepository : FonourRepositoryBase<Message>, IMessageRepository
    {
        public MessageRepository(MyDataBase dbContext) : base(dbContext)
        {

        }

        /// <summary>
        /// 标记为已读
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public ReceiveStatus UpdateMessage(int Id)
        {
            ReceiveStatus receiveStatus = new ReceiveStatus();
            try
            {
                var msg = new Message()
                {
                    Id = Id,
                    IsSee = 1,
                    ReadTime = DateTime.Now
                };
                _dbContext.Message.Attach(msg);
                _dbContext.Entry<Message>(msg).Property<int>(v => v.IsSee).IsModified = true;
                _dbContext.Entry<Message>(msg).Property<DateTime?>(v =>v.ReadTime).IsModified = true;
                _dbContext.SaveChanges();
                receiveStatus.msg = "更新成功";
            }
            catch (Exception ex)
            {
                receiveStatus.msg = "更新失败;" + ex.Message;
                receiveStatus.code = statuCode.Successful;
            }
            return receiveStatus;
        }
    }
}
